package com.hao.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.hao.viewdemo.R;

/**
 * Created by hao on 16/10/20.
 */
public class WithGesturesOverlapView extends FrameLayout implements WithGesturesView.OnScrollOut {

    private static final String TAG = WithGesturesOverlapView.class.getSimpleName();

    private LayoutInflater layoutInflater;
    private LinearLayout one;
    private WithGesturesView showImageOne;
    private LinearLayout tow;
    private WithGesturesView showImageTwo;
    private LinearLayout three;
    private WithGesturesView showImageThree;
    private WithGesturesView[] showImages;
    private WithGesturesView removeView;


    public WithGesturesOverlapView(Context context) {
        this(context, null);
    }

    public WithGesturesOverlapView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WithGesturesOverlapView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        layoutInflater = LayoutInflater.from(context);
        one = (LinearLayout) layoutInflater.inflate(R.layout.with_gestures_overlap_view, null);
        tow = (LinearLayout) layoutInflater.inflate(R.layout.with_gestures_overlap_view, null);
        three = (LinearLayout) layoutInflater.inflate(R.layout.with_gestures_overlap_view, null);
        showImageOne = (WithGesturesView) one.findViewById(R.id.show_image);
        showImageTwo = (WithGesturesView) tow.findViewById(R.id.show_image);
        showImageThree = (WithGesturesView) three.findViewById(R.id.show_image);
        showImageOne.setOnScrollOut(this);
        showImageTwo.setOnScrollOut(this);
        showImageThree.setOnScrollOut(this);
        showImages = new WithGesturesView[3];
        showImages[0] = showImageOne;
        showImages[1] = showImageTwo;
        showImages[2] = showImageThree;
        addAllView(one, tow, three);
    }

    @Override
    public void delete(View view) {
        for (int i = 0; i < showImages.length; i++) {
            if (showImages[i].getId() == view.getId()) {

            }
        }
        removeViewAt(0);
    }

    /**
     * 统一添加子view
     */
    private void addAllView(LinearLayout one, LinearLayout two, LinearLayout three) {
        showImageOne.setFocus(true);
        addView(one);
        addView(two);
        addView(three);
        LayoutParams layoutParamsOne = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        LayoutParams layoutParamsTwo = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        LayoutParams layoutParamsThree = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        layoutParamsOne.gravity = Gravity.CENTER;
        layoutParamsTwo.gravity = Gravity.CENTER;
        layoutParamsThree.gravity = Gravity.CENTER;

        showImageOne.getLayoutParams().width = 300;
        showImageOne.getLayoutParams().height = 400;
        showImageTwo.getLayoutParams().width = 260;
        showImageTwo.getLayoutParams().height = 360;
        showImageThree.getLayoutParams().width = 220;
        showImageThree.getLayoutParams().height = 320;
        one.setLayoutParams(layoutParamsOne);
        two.setLayoutParams(layoutParamsTwo);
        three.setLayoutParams(layoutParamsThree);
    }


}
